/* eslint-disable */
window.hkNativeCallBackList = {};

window.hkJSBridge = {
    call: hkAndroidNativeCall
};

String.prototype.hkNativehashCode = function () {
    var hash = 0;
    if (this.length == 0) return hash;
    for (var index = 0; index < this.length; index++) {
        var charactor = this.charCodeAt(index);
        hash = ((hash << 5) - hash) + charactor;
        hash = hash & hash;
    }
    return hash;
}


////log日志开关  -------------    需要再开     注意：这边开启可能会引起问题
//var console = {};
//console.log = function (message) {
//    window.hkJSBridge.call({
//        target: "logger",
//        action: "log",
//        data: message,
//        callback: null
//    });
//}



/*
 identifier:
 接口唯一标识，js通过唯一标识匹配到需要调用的js函数


 resultStatus
 1: 返回数据，并关闭通道
 2：返回数据，不关闭通道
 3：只是关闭通道

 resultData
 native返回给js的json格式字符串

*/
window.HKAppCallback = function (identifier, resultStatus, resultData) {

//return "11";

//return "000"+ identifier + resultStatus + resultData;

  console.log(resultData);

   var callBackfunc = window.hkNativeCallBackList[identifier];
  // callBackfunc(resultData);

    if (callBackfunc) {
       // console.log("resultStatus " + resultStatus);

        if (resultStatus < 3) {
            callBackfunc(resultData);
            //console.log("callBackfunc");
        }

        if (resultStatus != 2) {
            window.hkNativeCallBackList[identifier] = null;
            delete window.hkNativeCallBackList[identifier];
           // console.log("delete window.hkNativeCallBackList");
        }
    } else {
      //  console.log("identifier:" + identifier + " not exist");
      return "找不到id";
    }

}

////异常捕获的信息量比较少，可以采用Raven.js，类似腾讯bugly
//window.onerror = function (message, url, line, column, error) {
////  console.log('log---onerror::::',message, url, line, column, error);
//    console.log('js报错了…………………………');
//
//    console.log(message);
//    console.log(error);
//    console.log(line);
//}

function hkAndroidNativeCall(bridgeObjc) {

    var dataString = encodeURIComponent(JSON.stringify(bridgeObjc.data));
    var timestamp = Date.parse(new Date());
    var identifier = (bridgeObjc.target + bridgeObjc.action + dataString + timestamp).hkNativehashCode().toString();
    if(bridgeObjc.callback)
    {
        window.hkNativeCallBackList[identifier] = bridgeObjc.callback;
    }
    window.hkNativeAndroid.postMessage(
        bridgeObjc.target,
        bridgeObjc.action,
        JSON.stringify(bridgeObjc.data),
        identifier
    );

    // window.hkNativeAndroid.postMessage(
    //         bridgeObjc.target,
    //         bridgeObjc.action,
    //         JSON.stringify(bridgeObjc.data),
    //         identifier,  //android低版本 多了最后的逗号，导致调用报错
    // );
}
/*
native端 webview不使用的时候调用，比如复用缓存，当前页面结束的时候调用该方法
*/
window.hkNativeCallBackMethodManager = {
    removeAllCallBacks: function () {
        window.hkNativeCallBackList = {};
        console.log("hkNativeCallBackMethodManager removeAllCallBacks log");
    }
};
